package com.alibaba.algo.domain.user;

import cn.hutool.core.map.MapUtil;
import com.alibaba.algo.cooperation.feign.vo.req.SelectGroupPageRequestVO;
import com.alibaba.algo.cooperation.feign.vo.res.FeignEmployeeOrgInfoListVO;
import com.alibaba.algo.cooperation.usercomponent.vo.req.user.SelectEmployeeRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.res.user.EmployeeOrgResVO;
import com.alibaba.algo.cooperation.usercomponent.vo.res.user.EmployeeUserOrgVO;
import com.alibaba.algo.cooperation.usercomponent.vo.res.user.SelectEmployeeOrgByOrgIdResponseVO;
import com.alibaba.algo.cooperation.usercomponent.vo.res.user.SelectUserDetailResponseVO;
import com.alibaba.algo.core.base.BaseDomain;
import com.alibaba.algo.dao.user.SysEmployeeMapper;
import com.alibaba.algo.dao.user.SysEmployeeOrgMapper;
import com.alibaba.algo.entity.user.SysEmployee;
import com.alibaba.algo.entity.user.SysEmployeeOrg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 类SysEmployeeOrg.java的实现描述：员工和组织关联表Domain
 *
 * @author nieganggang 2021-03-12 16:08:17
 */
@Component
public class SysEmployeeOrgDomain extends BaseDomain<SysEmployeeOrg> {

    @Autowired
    private SysEmployeeDomain sysEmployeeDomain;

    @Resource
    private SysEmployeeMapper sysEmployeeMapper;

    @Autowired
    private SysOrgDomain sysOrgDomain;

    @Resource
    private SysEmployeeOrgMapper sysEmployeeOrgMapper;

    /**
     * 查询当前组织下的员工列表
     *
     * @param orgId 组织标识
     * @return 员工列表
     */
    public List<SelectEmployeeOrgByOrgIdResponseVO> selectEmployeeListByOrgId(Long orgId) {
        SysEmployeeOrg sysEmployeeOrgCon = new SysEmployeeOrg();
        sysEmployeeOrgCon.setOrgId(orgId);
        List<SysEmployeeOrg> sysEmployeeOrgList = this.listByPara(sysEmployeeOrgCon, new LinkedHashMap<>());
        // 员工列表
        List<Long> employeeIdList = sysEmployeeOrgList
                .stream()
                .filter(sysEmployeeOrg -> !ObjectUtils.isEmpty(sysEmployeeOrg.getEmployeeId()))
                .map(SysEmployeeOrg::getEmployeeId)
                .distinct()
                .collect(Collectors.toList());
        // 员工的集合
        Map<Long, SysEmployee> employeeMap = sysEmployeeDomain.qryEmployeeMapByIdList(employeeIdList);
        List<SelectEmployeeOrgByOrgIdResponseVO> resList = new ArrayList<>();
        for (Long employeeId : employeeIdList) {
            SelectEmployeeOrgByOrgIdResponseVO selectEmployeeOrgByOrgIdResponseVO = new SelectEmployeeOrgByOrgIdResponseVO();
            selectEmployeeOrgByOrgIdResponseVO.setOrgId(orgId);
            selectEmployeeOrgByOrgIdResponseVO.setEmployeeId(employeeId);
            SysEmployee sysEmployee = MapUtil.get(employeeMap, employeeId, SysEmployee.class, new SysEmployee());
            selectEmployeeOrgByOrgIdResponseVO.setEmployeeCode(sysEmployee.getEmployeeCode());
            selectEmployeeOrgByOrgIdResponseVO.setEmployeeName(sysEmployee.getEmployeeName());
            resList.add(selectEmployeeOrgByOrgIdResponseVO);
        }
        return resList;
    }

    /**
     * 查询组织列表下员工列表
     *
     * @param orgIdList 组织标识
     * @return 员工列表
     */
    public List<SysEmployee> selectEmployeeListByOrgIdList(List<Long> orgIdList) {
        if (CollectionUtils.isEmpty(orgIdList)) {
            return Collections.emptyList();
        }
        // 员工列表
        return sysEmployeeMapper.selectEmployeeListByOrgIdList(orgIdList);
    }

    /**
     * 查询组织列表下员工列表
     *
     * @param orgId 组织标识
     * @return 员工列表
     */
    public List<SysEmployee> selectCurAndLowerOrgEmployeeList(Long orgId) {
        if (null != orgId) {
            // 查询当前组织的所有下级组织
            List<Long> orgIdList = sysOrgDomain.qryOrgIdListByOrgId(String.valueOf(orgId));
            // 员工列表
            return sysEmployeeMapper.selectEmployeeListByOrgIdList(orgIdList);
        } else {
            return null;
        }
    }

    /**
     * 查询员工的组织列表 有效数据
     *
     * @param employeeId 员工标识
     * @return 组织列表
     */
    public List<EmployeeOrgResVO> selectEmployeeOrgListByEmployeeId(Long employeeId) {
        return sysEmployeeOrgMapper.selectEmployeeOrgListByEmployeeId(employeeId);
    }

    /**
     * 查询员工的组织列表 全部数据
     *
     * @param employeeId 员工标识
     * @return 组织列表
     */
    public List<EmployeeOrgResVO> selectEmployeeOrgListByEmployeeIdDefaultAll(Long employeeId,String isDeleted) {
        return sysEmployeeOrgMapper.selectEmployeeOrgListByEmployeeIdDefaultAll(employeeId,isDeleted);
    }

    public int deleteByEmployeeId(Long employeeId) {
        return sysEmployeeOrgMapper.deleteByEmployeeId(employeeId);
    }

    public Integer deleteByOrgId(Long orgId) {
        return sysEmployeeOrgMapper.deleteByOrgId(orgId);
    }

    public List<Long> selectCurAndLowerUserIdList(Long orgId) {
        return sysEmployeeOrgMapper.selectCurAndLowerUserIdList(orgId);
    }

    public List<Long> selectOrgIdListByCondition(
            String employeeName,
            String userAccount,
            String employeeCode,
            Long employeeId,
            String userName,
            Long userId,
            String searchKey) {
        return sysEmployeeOrgMapper.selectOrgIdListByCondition(
                employeeName,
                userAccount,
                employeeCode,
                employeeId,
                userName,
                userId,
                searchKey);
    }

    public List<Long> selectOrgIdListByEmployeeId(Long employeeId) {
        return sysEmployeeOrgMapper.selectOrgIdListByEmployeeId(employeeId);
    }

    public Long selectPrimaryOrgIdListByEmployeeId(Long employeeId) {
        return sysEmployeeOrgMapper.selectPrimaryOrgIdListByEmployeeId(employeeId);
    }

    public List<SelectUserDetailResponseVO> selectEmployeeListInOrgByName(SelectEmployeeRequestVO vo) {
        return sysEmployeeOrgMapper.selectEmployeeListInOrgByName(vo);
    }


    public void judgeAndInsert(Long employeeId, Long orgId) {
        SysEmployeeOrg sysEmployeeOrg = new SysEmployeeOrg();
        sysEmployeeOrg.setEmployeeId(employeeId);
        sysEmployeeOrg.setOrgId(orgId);
        Integer count = this.countByPara(sysEmployeeOrg);
        if (count > 0) {
            return;
        }
        LocalDateTime now = LocalDateTime.now();
        sysEmployeeOrg.setGmtCreated(now);
        sysEmployeeOrg.setGmtModified(now);
        insert(sysEmployeeOrg);
    }

    public Integer countByOrgIdList(List<Long> list){
        return sysEmployeeOrgMapper.countByOrgIdList(list);
    }

    public List<EmployeeUserOrgVO> selectEmployeeOrgListByCondition(SelectGroupPageRequestVO vo){
        return sysEmployeeOrgMapper.selectEmployeeOrgListByCondition(vo);
    }

    public Integer deleteByEmployeeIdAndOrgId(Long employeeId,Long orgId){
        return sysEmployeeOrgMapper.deleteByEmployeeIdAndOrgId(employeeId,orgId);
    }

    public Integer updateByEmployeeIdAndOrgId(SysEmployeeOrg sysEmployeeOrg){
        return sysEmployeeOrgMapper.updateByEmployeeIdAndOrgId(sysEmployeeOrg);
    }

    public Integer countByEmployeeIdAndOrgId(Long employeeId,Long orgId){
        return sysEmployeeOrgMapper.countByEmployeeIdAndOrgId(employeeId,orgId);
    }

    public List<FeignEmployeeOrgInfoListVO> selectPrimaryOrgInfoByEmployeeCodeList(List<String> employeeCodeList) {
        return sysEmployeeOrgMapper.selectPrimaryOrgInfoByEmployeeCodeList(employeeCodeList);
    }

    public List<FeignEmployeeOrgInfoListVO> selectPrimaryOrgInfoByEmployeeCodeListDefaultAll(String isDeleted,List<String> employeeCodeList) {
        return sysEmployeeOrgMapper.selectPrimaryOrgInfoByEmployeeCodeListDefaultAll(isDeleted, employeeCodeList);
    }

    public List<SysEmployeeOrg> selectListByParaStrSort(SysEmployeeOrg SysEmployeeOrg,String sort){
        return sysEmployeeOrgMapper.selectListByParaStrSort(SysEmployeeOrg,sort);
    }

    public Integer updateByEmployeeId(SysEmployeeOrg sysEmployeeOrg){
        return sysEmployeeOrgMapper.updateByEmployeeId(sysEmployeeOrg);
    }

    public Integer updateByOrgId(SysEmployeeOrg sysEmployeeOrg){
        return sysEmployeeOrgMapper.updateByOrgId(sysEmployeeOrg);
    }

    public Integer updateByEmployeeIdLastModified(SysEmployeeOrg sysEmployeeOrg, LocalDateTime lastModifyTime){
        return sysEmployeeOrgMapper.updateByEmployeeIdLastModified(sysEmployeeOrg,lastModifyTime);
    }
}
